﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace WebServer
{
    /// <summary>
    /// CSharpServer 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class CSharpServer : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string GetXML(string Type)
        {
            string _Result = "{'ErrorMsg':'数据访问错误'}";
            if (Type=="user")
            {
                _Result = @"[
                    {'LoginID':'guest','UserName':'测试','Depict':'来宾账号'},
                    {'LoginID':'admin','UserName':'系统','Depict':'系统管理员'},
                    {'LoginID':'DingY','UserName':'丁勇','Depict':'系统管理员'},
                    {'LoginID':'Test','UserName':'测试','Depict':'测试账号'},
                    {'LoginID':'kmdin','UserName':'KmDin','Depict':'丁丁技术服务中心'}
                ]";
            }

            return _Result;

            //Context.Response.ContentType = "application/json;utf-8";
            //Context.Response.Write(_Result);
        }

        [WebMethod]
        //[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public void GetJSON(string Type)
        {
            string _Result = "{\"ErrorMsg\":\"数据访问错误\"}";
            if (Type == "user")
            {
                _Result = "[{\"LoginID\":\"guest\",\"UserName\":\"测试\",\"Depict\":\"来宾账号\"},{ \"LoginID\":\"admin\",\"UserName\":\"系统\",\"Depict\":\"系统管理员\"},{ \"LoginID\":\"DingY\",\"UserName\":\"丁勇\",\"Depict\":\"系统管理员\"},{ \"LoginID\":\"Test\",\"UserName\":\"测试\",\"Depict\":\"测试账号\"},{ \"LoginID\":\"kmdin\",\"UserName\":\"KmDin\",\"Depict\":\"丁丁技术服务中心\"}]";
            }

            Context.Response.ContentType = "application/json;utf-8";
            Context.Response.AddHeader("Access-Control-Allow-Origin", "*");
            Context.Response.Write(_Result);
            Context.Response.End();
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void GetContent(string Title, string Content)
        {
            string _Result = "{\"Title\":\"" + HttpUtility.UrlDecode(Title) + "\",\"Content\":\"" + HttpUtility.UrlDecode(Content) + "\"}";

            Context.Response.ContentType = "application/json;utf-8";
            Context.Response.Write(_Result);
            Context.Response.End();
        }
        
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public void GetJSONP(string Info)
        {
            HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
            string CallBackFunName = HttpContext.Current.Request.Params["callback"] == null ? "callback" : HttpContext.Current.Request.Params["callback"].ToString();

            string _JSON = "{\"Info\":\"" + Info + "\"}";

            HttpContext.Current.Response.Write(string.Format("{0}({1})", CallBackFunName, _JSON));
            HttpContext.Current.Response.End();
        }
    }
}